package com.demo.behavioral.state;

import java.util.HashMap;
import java.util.Map;

public class VoteManager {
	
    //持有状体处理对象
    private VoteState state = null;
    
    //记录用户投票次数，Map<String,Integer>对应Map<用户名称，投票的次数>
    private Map<String,Integer> mapVoteCount = new HashMap<String,Integer>();
    
    public void logVote(String userName){
    	
        //1.为该用户增加投票次数
        //从记录中取出该用户已有的投票次数
        Integer oldVoteCount = mapVoteCount.get(userName);
        if(oldVoteCount == null){
            oldVoteCount = 0;
        }
        oldVoteCount += 1;
        mapVoteCount.put(userName, oldVoteCount);
        
        //2.判断该用户的投票类型，就相当于判断对应的状态
        if(oldVoteCount == 1){
            state = new NormalVoteState();
        }else if(oldVoteCount > 1 && oldVoteCount < 3){
            state = new RepeatVoteState();
        }else if(oldVoteCount > 2){
            state = new SpiteVoteState();
        }
        
        //然后转调状态对象来进行相应的操作
        state.getDetail(userName);
    }

}
